package com.asset.modules.wxApp;

import com.asset.modules.assets.model.Asset;
import com.asset.modules.assets.service.AssetService;
import com.asset.modules.common.model.ResponseBean;
import com.asset.modules.common.utils.Calcutil;
import com.asset.modules.common.utils.Constant;
import com.asset.modules.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author chenchen
 */
@RestController
@CrossOrigin
@RequestMapping("/wx")
@Api(tags = "微信小程序接口")
public class WxController {

    @Autowired
    AssetService as;

    @Value("{lowValue}")
    private String lowValue;

    @ApiOperation(value = "固定资产分门别类统计  checkedList：已经盘点集合；unCheckedList:未盘点集合； " +
            "discardList：已报废集合； lowValueList 低值易耗集合 ")
    @RequestMapping(value="/assetPartition", method = RequestMethod.GET)
    public ResponseBean getAssetPartition(String companyId){
        QueryWrapper<Asset> qw = new QueryWrapper<>();
        List<Asset> checkedList = new ArrayList<>();
        List<Asset> unCheckedList = new ArrayList<>();
        List<Asset> discardList = new ArrayList<>();
        List<Asset> lowValueList = new ArrayList<>();
        Map  result  = new HashMap();
        qw.eq("company_id", companyId);
        List<Asset> list = as.list(qw);
        if(list!=null&&list.size()>0){
            for(Asset a : list){
                if(a.getStatus().equals("-2")){
                    lowValueList.add(a);
                }else if(a.getStatus().equals("1")){
                    checkedList.add(a);
                }else if(a.getStatus().equals("-1")){
                    discardList.add(a);
                }else if(a.getStatus().equals("0")){
                    unCheckedList.add(a);
                }
            }
        }
        result.put("checked", checkedList);
        result.put("unChecked", unCheckedList);
        result.put("discard", discardList);
        result.put("lowValue", lowValueList);
        return new ResponseBean(HttpStatus.OK.value(), "请求成功！", result);
    }



    @ApiOperation(value="固资盘点")
    @RequestMapping(value = "/CheckAsset", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean checkAsset(@RequestParam String manageCode, @RequestParam long checkNum){
        QueryWrapper<Asset> qw = new QueryWrapper<>();
        String info;
        qw.eq("manage_code", manageCode);
        Asset a = as.getOne(qw);
        String status = a.getStatus();
        String year = a.getBuyTime().substring(0,4);
        String currentYear = DateUtils.formatDateToString(new Date(), DateUtils.DATE_FORMAT_Y);
        if(Constant.unCheckedStatus.equals(a.getStatus())){
            //检测是否是当年的低值易耗品
//            if(a.getRegisterMoney()<=Double.valueOf(lowValue)&&!year.equals(currentYear)){
//                info = "该设备为往期低值易耗品，不需要盘点！";
//            }
            //设置为已经盘点
            a.setStatus(Constant.checkedStatus);
            a.setCheckNum(checkNum);
            double earnMoney = Calcutil.calcEarn(a.getRegisterNum(), a.getRegisterMoney(), checkNum);
            a.setCheckMoney(earnMoney);
            as.saveOrUpdate(a);
            info = "盘点成功!";
        }else if(Constant.checkedStatus.equals(a.getStatus())){
            info = "该设备已盘点!";
        }else if(Constant.discardStatus.equals(a.getStatus())){
            info  = "该设备已报废!";
        }else if(Constant.lowValueStatus.equals(a.getStatus())) {
            info = "该设备为低值易耗品，不需要盘点!";
        }else{
            info = "设备状态未知";
        }
        return new ResponseBean(HttpStatus.OK.value(), "请求成功！", info);
    }


    @ApiOperation(value="固资报废")
    @RequestMapping(value = "/discardAsset", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean discardAsset(@RequestParam String manageCode){
        QueryWrapper<Asset> qw = new QueryWrapper<>();
        String info;
        qw.eq("manage_code", manageCode);
        Asset a = as.getOne(qw);
        String status = a.getStatus();
        String year = a.getBuyTime().substring(0,4);
        String currentYear = DateUtils.formatDateToString(new Date(), DateUtils.DATE_FORMAT_Y);
        //未盘点设备直接转入报废
        if(Constant.discardStatus.equals(a.getStatus())){
            info = "该设备已经报废!";
        }else{
            //其他状态设备直接设置为报废
            a.setStatus(Constant.discardStatus);
            info = "该设备状态设置为报废!";
        }

        return new ResponseBean(HttpStatus.OK.value(), "请求成功！", info);


    }
}
